/**
 * 调用方式 this.$httpPost(url,params,function(res){});
 * @param {Object} Vue
 * @param {Object} option
 */
import {
    DesEncode,
    DesDecode
} from '@/common/jeesite4_des.js';
exports.install = function(Vue, option) {

    /**
     * 群晖get封装
     * @param {Object} url
     * @param {Object} data
     */
    Vue.prototype.$get = function(url, data) {
        if (this.$config.sid) {
            data._sid = this.$config.sid
        }
        return new Promise((resolve, reject) => {
            uni.request({
                url: this.$config.host + url,
                data: data,
                header: {
                    'Accept': 'application/json',
                    'Content-Type': 'application/x-www-form-urlencoded', //自定义请求头信息
                    // #ifndef H5
                    'Cookie': this.$config.cookie,
                    // #endif
                },

                method: "GET",
                success: (response) => {
                    if (response.statusCode === 200) {
                        resolve(response.data)
                    } else {
                        reject(response)
                    }
                },
                fail: (error) => {
                    if (error && error.response) {
                        reject(error.response)
                        showError(error.response);
                    }
                },


            });
        })

    }

    /**
     * 群晖post封装
     * @param {Object} url
     * @param {Object} data
     */
    Vue.prototype.$post = function(url, data) {
        let _this = this;
        // #ifdef H5
        if (this.$config.sid) {
            if (!data) {
                data = {}
            }
            data.__token = this.$config.sid
        }
        // #endif
        return new Promise((resolve, reject) => {
            uni.request({
                url: this.$config.host + url,
                data: data,
                header: {
                    'Accept': 'application/json',
                    'Content-Type': 'application/json',
                    '__userId': this.$config.user.loginCode,
                    '__token': this.$config.sid
                },
                method: "POST",
                success: (response) => {
                    if (response.statusCode === 200) {
                        if (response.data.code == 401) {
                            //请重新登录
                            _this.$clearLoginInfo()
                            uni.reLaunch({
                                url: '/pages/login/index?statusCode=401'
                            })
                            reject(response)
                        }
                        resolve(response.data)
                    } else if (response.statusCode === 401) {
                        //请重新登录
                        _this.$clearLoginInfo()
                        uni.reLaunch({
                            url: '/pages/login/index?statusCode=401'
                        })
                        reject(response)
                    } else {
                        reject(response)
                    }
                },
                fail: (error) => {
                    if (error && error.response) {
                        reject(error.response)
                        showError(error.response);
                    }
                },
            });
        })
    }

    /**
     * 获取cookies
     */
    Vue.prototype.$getCookies = function(key) {
        if (this.$config.cookie) {
            const cookieArr = this.$config.cookie.split(';')
            for (let i = 0; i < cookieArr.length; i++) {
                if (cookieArr[i].indexOf('=') != -1) {
                    const cookieKey = cookieArr[i].substr(0, cookieArr[i].indexOf('='))
                    const cookieValue = cookieArr[i].substr(cookieArr[i].indexOf('=') + 1)
                    if (key == cookieKey) {
                        return cookieValue
                    }
                } else {
                    if (key == cookieArr[i]) {
                        return cookieArr[i];
                    }
                }
            }
        }
    }

    /**
     * 获取转换播放歌曲信息
     * @param {Object} val
     */
    Vue.prototype.$getPlayData = function(val) {
        this.$cache.loadMusicId = val.id
        this.$forceUpdate()

        const playData = {
            src: this.$config.host + '/api/music/getMusic?id=' + val.id +
                '&__userId=' + this.$config.user.loginCode +
                '&__token=' + this.$config.sid,
            title: val.title,
            singer: val.artist,
            artist: val.artist,
            coverImgUrl: this.$config.host + '/api/music/getMusicCoverById?id=' + val.coverId +
                '&__userId=' + this.$config.user.loginCode +
                '&__token=' + this.$config.sid,
            id: val.id,
            _data: val
        }

        this.$cache.loadMusicId = null
        return playData;
    }

    /**
     * 懒加载图片
     * @param {Object} data
     * @param {Object} item
     */
    Vue.prototype.$lazyLoadImage = function(data, item) {
        // console.log('[信息]图片加载完成', data, item)
        if (!item.lazyLoad) {
            item.cover = this.$config.host + '/api/music/getMusicCoverById?id=' + item.coverId +
                '&__userId' + this.$config.user.loginCode +
                '&__token' + this.$config.sid
            item.lazyLoad = true
            // this.$forceUpdate()

        }
    }


}
